Nature, October 2014
6 June 2016
Nature, October 2014
Least squares estimation
Linear regression model
\[ y_i = \alpha + \beta x_i + \epsilon \]
Estimates for the intercept and slope can be found by minimizing the squared residuals.
\[ \arg\min_{\alpha, \beta} \sum_i \left(y_i - (\alpha + \beta x_i) \right)^2 \]
Slidify
library(shiny)
runGitHub('ekstroem/ShinyLeastSquares')
The "role" of graphics is changing and expanding
animintMESS, MethComp, SuperRanker, …| Topic | Time |
|---|---|
| Introduction | 15 minutes |
| Highlevel interactive graphics (javascript libraries) | 35 minutes |
| Interactive graphics (shiny, ggplotly) | 35 minutes |
| Break | |
| Multi layer/-panel graphics | 30 minutes |
| Animated graphics | 30 minutes |
| Putting it all together | 30 minutes |
Tutorial goals:
Necesary packages etc XXX To be filled out at the end
Rstudio
See also: Toby's link
devtools::install_github("ramnathv/rCharts")
Tobys slides terms / discussion
D3.js doesn’t ship with any pre-built charts out of the box. However, go to website to get an overview of the possibilities.
Numerous possibilities in R packages
| library | function | |
|---|---|---|
| polychart | rPlot | ggplot2-like |
| Morris | mPlot | Pretty time series |
| nvd3 | nPlot | Lines/areas/bars |
| xCharts | xPlot | Various graphs |
| Highcharts | hPlot | Interactive charts |
| Leaflet | new() | Interactive Maps |
| Rickshaw | new() | Real-time ts graphs |
| Dimple | dPlot | Business analytics |
plot/lattice-like function.
library(rCharts)
library(MESS)
data(happiness)
mydf <- happiness
mydf$size <- sqrt(mydf$population)/8
r1 <- rPlot(x = "tax",
y = "happy",
data = mydf,
type = "point",
color = "continent",
size = "size")
r1
Type: bar, layers, …
plot/lattice-like function. Add layers and guides to plot ..
mydf2 <- data.frame(tax=c(1, 50),
happy=c(4.93, 7.09))
r1$layer(x = "tax",
y = "happy",
data = mydf2,
type = "line",
size=list(const=4))
r1$guides(y = list(title="New label",
min=0, max=12))
r1
layer \(\approx\) points/linesguides \(\approx\) axisaddParams \(\approx\) title, plot sizer3 <- rPlot(x = "tax", y = "happy",
data = mydf, type = "point",
color = "continent",
size = "size",
tooltip="#!function(item){
return ('Country: ' +item.country+
'
 Tax: ' +item.tax+
'% Happiness: ' +item.happy+
'<br> Pop: ' +item.population+
' mio.') }!#")
Passing javascript is non-trivial.
HighCharter
Mature charts, stocks (time series), maps. Good API.
library(highcharter)
hc <- highchart() %>%
hc_chart(type = "column") %>%
hc_title(text = "Denmark") %>%
hc_xAxis(categories = 2011:2016) %>%
hc_add_series(data = c(3806, 6184,
7557, 14792,
21315, 3016),
name = "Asylum seekers") %>%
hc_add_serie(name = "Syria",
data = c(429, 822,
1710, 7087,
8608, 777),
type = "spline")
hc
Data are added using one of the following functions
| Name | Function |
|---|---|
| hc_add_series | Add single series (named: data, name) |
| hc_add_series_list | Add list named series |
| hc_add_series_df | Add data.frame. Name variables accordingly |
| hc_add_series_ts | Add ts object. Extra argument: name |
| hc_add_series_scatter | Crate scatter from two vectors. Extra arguments (size, color, label) |
| hc_add_series_boxplot | Add boxplots |
| hc_add_series_map | Add geojson map |
| Function | Example |
|---|---|
| Title | hc_title(text = "Temperature") |
| Subtitle | hc_subtitle(text = "Somewhere warm") |
| Axis | hc_xAxis(categories = month) |
| hc_yAxis(title = list(text = "Temperature"), labels = list(format = "{value} C")) | |
| Theme | hc_add_theme(hc_theme_sandsignika()) |
| Zoom | hc_chart(zoomType = "xy") |
| Tooltip | hc_tooltip(useHTML = TRUE, headerFormat="<table>", |
| pointFormat = "<tr><th>x-val</th><td>{point.x}</td></tr>" | |
| footerFormat = "</table>") | |
| Legend | hc_legend(enabled=FALSE) |
| Credits | hc_credits(enabled = TRUE, text = "Source: Wikipedia", href = "https://wikipedia.com", style = list(fontSize = "12px")) |
happiness$size <- sqrt(happiness$population)/8
happy2 <- highchart() %>%
hc_title(text = "Taxes and happiness") %>%
hc_subtitle(text = "Source: Wikipedia") %>%
hc_xAxis(title = list(text = "Taxation (%)")) %>%
hc_yAxis(title = list(text = "Happiness")) %>%
hc_chart(zoomType = "xy") %>%
hc_add_serie_scatter(happiness$tax, happiness$happy,
happiness$size, happiness$continent,
happiness$country,
dataLabels = list(
enabled = TRUE,
format = "{point.label}"
),
extrainfo = sprintf("Mostly %s", happiness$religion)
) %>%
hc_tooltip(useHTML = TRUE,
headerFormat = "<table>",
pointFormat = paste("<tr><th colspan=\"1\"><b>{point.label}</b></th></tr>",
"<tr><th>Weight</th><td>{point.x} lb/1000</td></tr>",
"<tr><th>MPG</th><td>{point.y} mpg</td></tr>",
"<tr><th>Drat</th><td>{point.z} </td></tr>",
"<tr><th>HP</th><td>{point.valuecolor} hp</td></tr>",
"<tr><th>HP</th><td>{point.extrainfo} hp</td></tr>"),
footerFormat = "</table>")
happy2
More on functions?
Same build-up as highcharter
library(dygraphs)
head(nhtemp)
dygraph(nhtemp, main = "New Haven Temperatures") %>%
dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))
| x-val | {point.x} |
|---|
") | | Legend | hc_legend(enabled=FALSE) | | Credits | hc_credits(enabled = TRUE, text = "Source: Wikipedia", href = "https://wikipedia.com", style = list(fontSize = "12px")) |
Should be the same as before (happyness)
library(RColorBrewer) tmp <- data.frame(year=seq(1790, 1970, 10), uspop=as.numeric(uspop)) tmp %>% mjs_plot(x=year, y=uspop) %>% mjs_point() %>% mjs_add_marker(1850, "Something Wonderful") %>% mjs_add_baseline(150, "Something Awful")
| x-val | {point.x} |
|---|
") | | Legend | hc_legend(enabled=FALSE) | | Credits | hc_credits(enabled = TRUE, text = "Source: Wikipedia", href = "https://wikipedia.com", style = list(fontSize = "12px")) |
library(leaflet) library(MESS) data(earthquakes) m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=earthquakes$longitude[earthquakes$mag>6], lat=earthquakes$latitude[earthquakes$mag>6], popup="Mag 6+") m
<div class='container'>
<input id='slider' type='range' min=1960 max=2010 ng-model='year' width=200>
<span ng-bind='year'></span>
<div id='chart7bc34e2b40bd' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.year = 1960;
$scope.$watch('year', function(newYear){
mapchart7bc34e2b40bd.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>
Note: html/js output. Need javascript machinery to run (ie., html with js code embedded or online link).
library(htmlwidgets) saveWidget(widget, file, selfcontained = TRUE, background="white")
iframe
## <script type='text/javascript' src=/Users/cld189/Library/R/3.3/library/rCharts/libraries/polycharts/js/polychart2.standalone.js></script>
## <style>
## .rChart {
## display: block;
## margin-left: auto;
## margin-right: auto;
## width: 400px;
## height: 500px;
## }
## </style>
## <div id = 'chart7bc324848bb1' class = 'rChart polycharts'></div>
## <script type='text/javascript'>
## var chartParams = {
## "dom": "chart7bc324848bb1",
## "width": 400,
## "height": 500,
## "layers": [
## {
## "x": "tax",
## "y": "happy",
## "data": {
## "country": [ "Andorra", "United Arab Emirate", "Afghanistan", "Albania", "Armenia", "Angola", "Argentina", "Austria", "Australia", "Azerbaijan", "Bosnia Herzegovina", "Bangladesh", "Belgium", "Burkina Faso", "Bulgaria", "Burundi", "Benin", "Bolivia", "Brazil", "Botswana", "Belarus", "Belize", "Canada", "Dem. Rep. of Congo", "Central African Rep", "Switzerland", "Ivory Coast", "Chile", "Cameroon", "China", "Colombia", "Costa Rica", "Cyprus", "Czech Republic", "Germany", "Djibouti", "Denmark", "Dominican Republic", "Algeria", "Ecuador", "Estonia", "Egypt", "Spain", "Ethiopia", "Finland", "France", "United Kingdom", "Georgia", "Ghana", "Guinea", "Greece", "Guatemala", "Guyana", "Hong Kong", "Honduras", "Croatia", "Haiti", "Hungary", "Indonesia", "Ireland", "Israel", "India", "Iraq", "Iran", "Iceland", "Italy", "Jamaica", "Jordan", "Japan", "Kenya", "Kyrgyzstan", "Cambodia", "Kosovo", "South Korea", "Kuwait", "Kazakhstan", "Laos", "Lebanon", "Sri Lanka", "Liberia", "Lithuania", "Luxembourg", "Latvia", "Morocco", "Moldova", "Montenegro", "Madagascar", "Macedonia", "Mali", "Mongolia", "Mauritania", "Malta", "Malawi", "Mexico", "Malaysia", "Mozambique", "Namibia", "Niger", "Nigeria", "Nicaragua", "Netherlands", "Norway", "Nepal", "New Zealand", "Panama", "Peru", "Philippines", "Pakistan", "Poland", "Palestina", "Portugal", "Paraguay", "Qatar", "Romania", "Serbia", "Russia", "Rwanda", "Saudi Arabia", "Sudan", "Sweden", "Singapore", "Slovenia", "Slovakia", "Sierra Leone", "Senegal", "El Salvador", "Syria", "Chad", "Togo", "Thailand", "Tajikistan", "Turkmenistan", "Tunisia", "Turkey", "Trinidad and Tobago", "Taiwan", "Tanzania", "Ukraine", "Uganda", "United States", "Uruguay", "Uzbekistan", "Venezuela", "Vietnam", "Yemen", "South Africa", "Zambia", "Zimbabwe" ],
## "happy": [ 6.8, 7.3, 4.1, 4.6, 5, 4.3, 7.3, 7.6, 7.7, 5.3, 5.6, 5.3, 7.3, 4.4, 4.4, 2.9, 3, 6.5, 7.5, 4.7, 5.2, 6.6, 7.8, 4.4, 4.6, 8, 4.4, 6.6, 3.9, 6.3, 7.7, 8.5, 7.1, 6.5, 7.1, 5.7, 8.3, 7.6, 5.4, 6.4, 6, 5.7, 7.2, 4.2, 7.9, 6.6, 7.1, 4.3, 5.2, 4.5, 6.4, 7.2, 6.5, 6.6, 7, 6, 3.9, 5.5, 6.3, 7.6, 7, 5.5, 4.7, 5.9, 8.2, 6.7, 6.7, 5.9, 6.5, 3.7, 5.5, 4.9, 5.4, 6, 6.6, 6.1, 6.2, 4.7, 5.1, 4.3, 5.5, 7.7, 5.4, 5.4, 4.9, 5.2, 3.7, 4.7, 4.7, 5.7, 4.9, 7.1, 6.2, 7.9, 6.5, 3.8, 5.2, 3.8, 5.7, 7.1, 7.6, 7.9, 5.3, 7.5, 7.8, 6.2, 5.9, 5, 6.4, 4.9, 5.7, 6.9, 6.8, 5.7, 5.4, 5.5, 4.3, 6.5, 5, 7.8, 6.9, 6.9, 5.9, 3.5, 4.5, 6.7, 5.9, 5.4, 2.6, 6.6, 5.1, 7.2, 5.9, 5.6, 7, 6.2, 2.8, 5, 4.8, 7.4, 6.8, 6, 7.5, 6.1, 4.8, 5.8, 5, 3 ],
## "tax": [ null, 1.4, 6.4, 22.9, 14.1, 5.7, 37.2, 43.4, 30.8, 17.8, 41.2, 8.5, 46.8, 11.5, 34.4, 17.4, 15.4, 27, 34.4, 35.2, 24.2, 21.6, 35.2, 5.9, 7.7, 29.4, 15.3, 18.6, 18.2, 17, 23, 14, 39.2, 36.3, 40.6, 20, 49, 15, 7.7, 13.2, 32.3, 15.8, 37.3, 11.6, 43.6, 44.6, 39, 21.7, 20.8, 8.2, 33.5, 11.9, 31.9, 13, 15.6, 26.6, 9.4, 39.1, 12, 30.8, 36.8, 17.7, null, 6.1, 40.4, 42.6, 27.2, 21.1, 28.3, 18.4, 21.4, 8, null, 26.8, 1.5, 26.8, 10.8, 14.4, 15.3, 13.2, 20.9, 36.5, 30.4, 22.3, 33.8, 28, 10.7, 29.3, 15.3, 33.8, 15.4, 35.2, 20.7, 29.7, 15.5, 13.4, 28.8, 11, 6.1, 17.8, 39.8, 43.6, 10.9, 34.5, 10.6, 15.1, 14.4, 10.2, 33.8, null, 37, 12, 2.2, 28.1, 34.1, 36.9, 14.1, 5.3, 6.3, 47.9, 14.2, 39.3, 29.5, 10.5, 19.2, 13.3, 10.7, 4.2, 15.5, 17, 16.5, 20.2, 14.9, 32.5, 28, 12.4, 12, 38.1, 12.6, 26.9, 23.1, 21, 25, 13.8, 7.1, 26.9, 16.1, 49.3 ],
## "religion": [ "Christian", "Muslim", "Muslim", "Muslim", "Christian", "Christian", "Christian", "Christian", "Christian", "Muslim", "Christian", "Muslim", "Christian", "Muslim", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "Christian", "None", "Christian", "Christian", "None", "Christian", "Christian", "Christian", "None", "Christian", "Muslim", "Christian", "Christian", "Muslim", "Christian", "None", "Muslim", "Christian", "Christian", "Christian", "None", "Christian", "Christian", "Christian", "Muslim", "Christian", "Christian", "Christian", "Buddhist", "Christian", "Christian", "Christian", "Christian", "Muslim", "Christian", "Other", "Hindu", "Muslim", "Muslim", "Christian", "Christian", "Christian", "Muslim", "Buddhist", "Christian", "Muslim", "Buddhist", null, "None", "Muslim", "Muslim", "Buddhist", "Muslim", "Buddhist", "Christian", "Christian", "Christian", "Christian", "Muslim", "Christian", "Christian", "Other", "Christian", "Muslim", "None", "Muslim", "Christian", "Christian", "Christian", "Muslim", "Christian", "Christian", "Muslim", "Muslim", "Christian", "None", "Christian", "Hindu", "Christian", "Christian", "Christian", "Christian", "Muslim", "Christian", "Muslim", "Christian", "Muslim", "Muslim", "Christian", "Christian", "Christian", "Christian", "Muslim", "Muslim", "Christian", "Buddhist", "Christian", "Christian", "Muslim", "Muslim", "Christian", "Muslim", "Muslim", "Other", "Buddhist", "Muslim", "Muslim", "Muslim", "Muslim", "Christian", "Buddhist", "Christian", "Christian", "Christian", "Christian", "Christian", "Muslim", "Christian", "Buddhist", "Muslim", "Christian", "Christian", "Christian" ],
## "continent": [ "EU", "AS", "AS", "EU", "EU", "AF", "SA", "EU", "OC", "EU", "EU", "AS", "EU", "AF", "EU", "AF", "AF", "SA", "SA", "AF", "EU", "NA", "NA", "AF", "AF", "EU", "AF", "SA", "AF", "AS", "SA", "NA", "EU", "EU", "EU", "AF", "EU", "NA", "AF", "SA", "EU", "AF", "EU", "AF", "EU", "EU", "EU", "EU", "AF", "AF", "EU", "NA", "SA", "AS", "NA", "EU", "NA", "EU", "AS", "EU", "AS", "AS", "AS", "AS", "EU", "EU", "NA", "AS", "AS", "AF", "AS", "AS", "EU", "AS", "AS", "AS", "AS", "AS", "AS", "AS", "EU", "EU", "EU", "AF", "EU", "EU", "AF", "EU", "AF", "AS", "AF", "EU", "AF", "NA", "AS", "AF", "AF", "AF", "AF", "NA", "EU", "EU", "AS", "OC", "NA", "SA", "AS", "AS", "EU", "AS", "EU", "SA", "AS", "EU", "EU", "EU", "AF", "AS", "AF", "EU", "AS", "EU", "EU", "AF", "AF", "NA", "AS", "AF", "AF", "AS", "AS", "AS", "AF", "AS", "NA", "AS", "AF", "EU", "AF", "NA", "SA", "AS", "SA", "AS", "AS", "AF", "AF", "AF" ],
## "population": [ 0.078, 8.264, 25.5, 2.831, 3.268, 20.609, 40.117, 8.452, 22.687, 9.235, 3.839, 152.52, 10.951, 15.73, 7.364, 8.749, 9.352, 10.426, 192.38, 2.038, 9.458, 0.312, 34.875, 69.575, 4.576, 7.952, 20.595, 17.402, 19.406, 1347.3, 46.631, 4.301, 0.838, 10.504, 81.859, 0.923, 5.584, 9.445, 37.1, 14.483, 1.294, 82.448, 46.185, 84.32, 5.413, 65.35, 62.262, 4.497, null, 10.481, 10.787, 14.713, 0.784, 7.103, 8.385, 4.29, 10.085, 9.962, 237.64, 4.588, 7.879, 1210.2, 33.33, 75.149, 0.32, 60.813, 2.705, 6.329, 127.53, 42.749, 5.477, 14.479, null, 48.58, 3.582, 16.734, 6.465, 4.292, 20.277, 4.245, 3.187, 0.511, 2.07, 32.628, 3.559, 0.62, 20.696, 2.0557, 16.319, 2.844, 3.378, 0.417, 15.883, 112.34, 28.334, 23.7, 2.364, 16.274, 166.63, 5.815, 16.736, 5.026, 26.62, 4.434, 3.405, 30.135, 92.337, 180.27, 38.501, 4.293, 10.561, 6.337, 1.699, 19.042, 7.12, 143.12, 10.718, 27.136, 30.894, 9.495, 5.183, 2.057, 5.445, 6.126, 12.855, 6.183, 21.684, 11.831, 6.191, 65.479, 7.8, 5.17, 10.673, 74.724, 1.317, 23.261, 43.188, 45.589, 32.939, 314.03, 3.251, 29.123, 27.15, 87.84, 24.527, 50.586, 13.046, 13.014 ],
## "size": [ 0.034911, 0.35934, 0.63122, 0.21032, 0.22597, 0.56746, 0.79172, 0.3634, 0.59539, 0.37986, 0.24492, 1.5437, 0.41365, 0.49576, 0.33921, 0.36973, 0.38226, 0.40362, 1.7337, 0.17845, 0.38442, 0.069821, 0.73819, 1.0426, 0.26739, 0.35249, 0.56727, 0.52145, 0.55065, 4.5883, 0.85359, 0.25924, 0.11443, 0.40512, 1.1309, 0.12009, 0.29538, 0.38416, 0.76137, 0.47571, 0.14219, 1.135, 0.84949, 1.1478, 0.29082, 1.0105, 0.98633, 0.26508, null, 0.40468, 0.41054, 0.47947, 0.11068, 0.33314, 0.36196, 0.2589, 0.39696, 0.39453, 1.927, 0.26775, 0.35087, 4.3485, 0.72165, 1.0836, 0.070711, 0.97478, 0.20559, 0.31447, 1.4116, 0.81728, 0.29254, 0.47564, null, 0.87124, 0.23658, 0.51134, 0.31783, 0.25896, 0.56287, 0.25754, 0.22315, 0.089355, 0.17984, 0.71401, 0.23582, 0.098425, 0.56866, 0.17922, 0.50496, 0.2108, 0.22974, 0.080719, 0.49817, 1.3249, 0.66537, 0.60853, 0.19219, 0.50426, 1.6136, 0.30143, 0.51137, 0.28023, 0.64493, 0.26321, 0.23066, 0.68619, 1.2012, 1.6783, 0.77561, 0.25899, 0.40622, 0.31467, 0.16293, 0.54546, 0.33354, 1.4954, 0.40923, 0.65115, 0.69478, 0.38517, 0.28458, 0.17928, 0.29168, 0.30938, 0.44817, 0.31082, 0.58208, 0.42995, 0.31102, 1.0115, 0.34911, 0.28422, 0.40837, 1.0805, 0.14345, 0.60287, 0.82147, 0.844, 0.71741, 2.2151, 0.22538, 0.67457, 0.65132, 1.1715, 0.61906, 0.88905, 0.45149, 0.45094 ]
## },
## "facet": null,
## "type": "point",
## "color": "continent",
## "size": "size"
## }
## ],
## "facet": [],
## "guides": [],
## "coord": [],
## "id": "chart7bc324848bb1"
## }
## _.each(chartParams.layers, function(el){
## el.data = polyjs.data(el.data)
## })
## var graph_chart7bc324848bb1 = polyjs.chart(chartParams);
## </script>
Differences to other libraries … public
ggiraph
Use standard R. Rather simple and beautiful. Simple input - no fancy transitions. Requires server
library(shiny)
runGitHub('ekstroem/ShinySampleMean')
ui.R
server.R
Running apps from Github